package appreate.sixnations.quiz;

import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.CountDownTimer;
import android.os.Handler;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import appreate.sixnations.quiz.R;
import appreate.sixnations.quiz.utils.Commons;
import appreate.sixnations.quiz.utils.CustomTypefaceSpan;

public abstract class SuperQuiz extends Activity {

	protected ArrayList<String> history = new ArrayList<String>();
	protected CountDownTimer countTimer;
	protected TextView mProgress;
	protected int win;
	protected int lose;
	protected Handler mHandler = new Handler();
	protected Typeface mTypeface;
	protected String mNameFlag3;
	protected String mNameFlag2;
	protected String mNameFlag1;
	protected String mNameFlag4;
	protected Button mButtonQuattro;
	protected Button mButtonDue;
	protected Button mButtonTre;
	protected Button mButtonUno;
	protected String rightAswer;
	protected int mScores;
	private TextView mScore;
	protected int mMode;

	private static final int QUIZNAMETOFLAG = 1;
	private static final int QUIZFLAGTONAME = 2;
	private static final int QUIZSURVIVAL = 3;

	protected void setCounterTimer(final boolean fromQuizNameToFlag) {
		countTimer = new CountDownTimer(61000, 1000) {

			public void onTick(long millisUntilFinished) {
				mProgress.setText((millisUntilFinished / 1000) + "");

				if ((millisUntilFinished / 1000) < 10) {
					mProgress.setTextColor(getResources().getColor(R.color.red));
				} else if ((millisUntilFinished / 1000) < 20) {
					mProgress.setTextColor(getResources().getColor(R.color.yellow));
				}

			}

			public void onFinish() {
				mProgress.setText(0 + "");
				mScores = 3 * win - lose;

				String title = getString(R.string.score).replace("%score", mScores + "");
				SpannableStringBuilder ssBuilser = new SpannableStringBuilder(title);
				CustomTypefaceSpan spanCustom = new CustomTypefaceSpan(Typeface.MONOSPACE.toString(), mTypeface);
				ssBuilser.setSpan(spanCustom, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

				String ok = "OK";
				SpannableStringBuilder ssBuilserOk = new SpannableStringBuilder(ok);
				ssBuilserOk.setSpan(spanCustom, 0, ok.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

				AlertDialog.Builder builder = new AlertDialog.Builder(SuperQuiz.this);
				builder.setMessage(ssBuilser).setCancelable(false)
						.setPositiveButton(ssBuilserOk, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {

								if (fromQuizNameToFlag)
									saveRecord(QUIZNAMETOFLAG);
								else
									saveRecord(QUIZFLAGTONAME);
								SuperQuiz.this.finish();
							}
						});
				AlertDialog alert = builder.create();
				alert.show();
			}
		}.start();
	}

	protected void init() {

		mMode = getIntent().getIntExtra("mode", 0);

		mTypeface = Typeface.createFromAsset(getAssets(), "fonts/wanted.ttf");
		mButtonUno = (Button) findViewById(R.id.uno);
		mButtonUno.setTypeface(mTypeface);
		mButtonDue = (Button) findViewById(R.id.due);
		mButtonDue.setTypeface(mTypeface);
		mButtonTre = (Button) findViewById(R.id.tre);
		mButtonTre.setTypeface(mTypeface);
		mButtonQuattro = (Button) findViewById(R.id.quattro);
		mButtonQuattro.setTypeface(mTypeface);

		mScore = (TextView) findViewById(R.id.score);
		mScore.setTypeface(mTypeface);
		mProgress = (TextView) findViewById(R.id.progress);
		mProgress.setTypeface(mTypeface);

		mButtonUno.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mNameFlag1.equals(rightAswer)) {
					reset(mButtonUno);
				} else {
					lose(mButtonUno);
				}
			}
		});

		mButtonDue.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mNameFlag2.equals(rightAswer)) {
					reset(mButtonDue);
				} else {
					lose(mButtonDue);
				}
			}
		});

		mButtonTre.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mNameFlag3.equals(rightAswer)) {
					reset(mButtonTre);
				} else {
					lose(mButtonTre);
				}
			}
		});

		mButtonQuattro.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mNameFlag4.equals(rightAswer)) {
					reset(mButtonQuattro);
				} else {
					lose(mButtonQuattro);
				}
			}
		});

	}

	public void reset(Button button) {
		enableButton(false);
		win++;
		updateScore();

		button.setBackgroundResource(getDrawableY(button));
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(600);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				mHandler.post(new Runnable() {
					@Override
					public void run() {
						enableButton(true);
						setFlags();
						setFlagWin();
					}
				});
			}
		});
		t.start();
	}

	private void lose(Button button) {
		button.setEnabled(false);
		lose++;
		decrLife(false);
		button.setBackgroundResource(getDrawableX(button));
		updateScore();
	}

	private void updateScore() {
		mScore.setVisibility(View.VISIBLE);
		mScore.setText(getString(R.string.score_now).replace("%score", 3 * win - lose + ""));

	}

	protected abstract void setFlags();

	protected abstract void decrLife(boolean zero);

	protected abstract void setFlagWin();

	protected abstract int getDrawableX(Button button);

	protected abstract int getDrawableY(Button button);

	private void enableButton(boolean enable) {
		mButtonUno.setEnabled(enable);
		mButtonDue.setEnabled(enable);
		mButtonTre.setEnabled(enable);
		mButtonQuattro.setEnabled(enable);
	}

	@Override
	public void onBackPressed() {
		if (countTimer != null)
			countTimer.cancel();
		super.onBackPressed();
	}

	protected void saveRecord(int mode) {
		SharedPreferences recordPref = getSharedPreferences("record", Activity.MODE_PRIVATE);
		if (recordPref != null) {
			ArrayList<Integer> record = new ArrayList<Integer>();
			switch (mode) {
			case QUIZNAMETOFLAG:
				if (mMode == Commons.EASY) {
					record.add(recordPref.getInt("recordName11", 0));
					record.add(recordPref.getInt("recordName21", 0));
					record.add(recordPref.getInt("recordName31", 0));
				} else if (mMode == Commons.NORMAL) {
					record.add(recordPref.getInt("recordName12", 0));
					record.add(recordPref.getInt("recordName22", 0));
					record.add(recordPref.getInt("recordName32", 0));
				}/* else if (mMode == Commons.HARD) {
					record.add(recordPref.getInt("recordName13", 0));
					record.add(recordPref.getInt("recordName23", 0));
					record.add(recordPref.getInt("recordName33", 0));
				}*/ else {
					record.add(recordPref.getInt("recordName1", 0));
					record.add(recordPref.getInt("recordName2", 0));
					record.add(recordPref.getInt("recordName3", 0));
				}
				break;
			case QUIZFLAGTONAME:
				if (mMode == Commons.EASY) {
					record.add(recordPref.getInt("recordFlag11", 0));
					record.add(recordPref.getInt("recordFlag21", 0));
					record.add(recordPref.getInt("recordFlag31", 0));
				} else if (mMode == Commons.NORMAL) {
					record.add(recordPref.getInt("recordFlag12", 0));
					record.add(recordPref.getInt("recordFlag22", 0));
					record.add(recordPref.getInt("recordFlag32", 0));
				} /*else if (mMode == Commons.HARD) {
					record.add(recordPref.getInt("recordFlag13", 0));
					record.add(recordPref.getInt("recordFlag23", 0));
					record.add(recordPref.getInt("recordFlag33", 0));
				}*/ else {
					record.add(recordPref.getInt("recordFlag1", 0));
					record.add(recordPref.getInt("recordFlag2", 0));
					record.add(recordPref.getInt("recordFlag3", 0));
				}
				break;
			case QUIZSURVIVAL:
				if (mMode == Commons.EASY) {
					record.add(recordPref.getInt("recordSurvival11", 0));
					record.add(recordPref.getInt("recordSurvival21", 0));
					record.add(recordPref.getInt("recordSurvival31", 0));
				} else if (mMode == Commons.NORMAL) {
					record.add(recordPref.getInt("recordSurvival12", 0));
					record.add(recordPref.getInt("recordSurvival22", 0));
					record.add(recordPref.getInt("recordSurvival32", 0));
				}/* else if (mMode == Commons.HARD) {
					record.add(recordPref.getInt("recordSurvival13", 0));
					record.add(recordPref.getInt("recordSurvival23", 0));
					record.add(recordPref.getInt("recordSurvival33", 0));
				} */else {
					record.add(recordPref.getInt("recordSurvival1", 0));
					record.add(recordPref.getInt("recordSurvival2", 0));
					record.add(recordPref.getInt("recordSurvival3", 0));
				}
				break;

			default:
				break;
			}
			record.add(mScores);

			Collections.sort(record);

			SharedPreferences.Editor editor = recordPref.edit();

			switch (mode) {
			case QUIZNAMETOFLAG:
				if (mMode == Commons.EASY) {
					editor.putInt("recordName11", record.get(3));
					editor.putInt("recordName21", record.get(2));
					editor.putInt("recordName31", record.get(1));
				} else if (mMode == Commons.NORMAL) {
					editor.putInt("recordName12", record.get(3));
					editor.putInt("recordName22", record.get(2));
					editor.putInt("recordName32", record.get(1));
				}/* else if (mMode == Commons.HARD) {
					editor.putInt("recordName13", record.get(3));
					editor.putInt("recordName23", record.get(2));
					editor.putInt("recordName33", record.get(1));
				}*/ else {
					editor.putInt("recordName1", record.get(3));
					editor.putInt("recordName2", record.get(2));
					editor.putInt("recordName3", record.get(1));
				}
				break;
			case QUIZFLAGTONAME:
				if (mMode == Commons.EASY) {
					editor.putInt("recordFlag11", record.get(3));
					editor.putInt("recordFlag21", record.get(2));
					editor.putInt("recordFlag31", record.get(1));
				} else if (mMode == Commons.NORMAL) {
					editor.putInt("recordFlag12", record.get(3));
					editor.putInt("recordFlag22", record.get(2));
					editor.putInt("recordFlag32", record.get(1));
				}/* else if (mMode == Commons.HARD) {
					editor.putInt("recordFlag13", record.get(3));
					editor.putInt("recordFlag23", record.get(2));
					editor.putInt("recordFlag33", record.get(1));
				}*/ else {
					editor.putInt("recordFlag1", record.get(3));
					editor.putInt("recordFlag2", record.get(2));
					editor.putInt("recordFlag3", record.get(1));
				}
				break;
			case QUIZSURVIVAL:
				if (mMode == Commons.EASY) {
					editor.putInt("recordSurvival11", record.get(3));
					editor.putInt("recordSurvival21", record.get(2));
					editor.putInt("recordSurvival31", record.get(1));
				} else if (mMode == Commons.NORMAL) {
					editor.putInt("recordSurvival12", record.get(3));
					editor.putInt("recordSurvival22", record.get(2));
					editor.putInt("recordSurvival32", record.get(1));
				}/* else if (mMode == Commons.HARD) {
					editor.putInt("recordSurvival13", record.get(3));
					editor.putInt("recordSurvival23", record.get(2));
					editor.putInt("recordSurvival33", record.get(1));
				}*/ else {
					editor.putInt("recordSurvival1", record.get(3));
					editor.putInt("recordSurvival2", record.get(2));
					editor.putInt("recordSurvival3", record.get(1));
				}
				break;
			default:
				break;
			}
			editor.commit();

		}
	}

}
